package com.sharad.quizbowl.ui.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class DummyServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7034771980262659873L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		HttpClient httpclient = new DefaultHttpClient();
		HttpGet httpget = new HttpGet(
				"http://ec2-50-17-57-153.compute-1.amazonaws.com:8080/Quizbowl_UI/shakespeare.txt");
		try {
			org.apache.http.HttpResponse r;
			r = httpclient.execute(httpget);
			HttpEntity entity = r.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				@SuppressWarnings("unused")
				int l = 0;
				byte[] tmp = new byte[1000];
				StringBuilder sb = new StringBuilder();
				while ((l = instream.read(tmp)) != -1) {
					sb.append(new String(tmp));
				}
				String text = sb.toString();
				httpget = new HttpGet(
						"http://ec2-50-17-57-153.compute-1.amazonaws.com:8080/Quizbowl_UI/characters.txt");
				r = httpclient.execute(httpget);
				entity = r.getEntity();
				sb = new StringBuilder();
				if (entity != null) {
					instream = entity.getContent();
					tmp = new byte[1000];
					while ((l = instream.read(tmp)) != -1) {
						sb.append(new String(tmp));
					}
				}
				String characters = sb.toString();

				String[] temp = text.replaceAll("\n\n", "\n")
						.replaceAll("\n\n", "\n").split("\n");
				List<String> list = new ArrayList<String>();
				String temporary = "";
				for (int i = 0; i < temp.length; i++) {
					boolean allcaps = true;
					for (int j = 0; j < temp[i].length(); j++) {
						if ((temp[i].charAt(j) + "").matches("[a-z]")) {
							allcaps = false;
						}
					}

					if (!allcaps) {
						temporary += temp[i] + "\n";

						if (temp[i].charAt(temp[i].length() - 1) == ';'
								|| temp[i].charAt(temp[i].length() - 1) == '?'
								|| temp[i].charAt(temp[i].length() - 1) == '.'
								|| temp[i].charAt(temp[i].length() - 1) == '!') {
							list.add(temporary);
							temporary = "";
						}

					}

				}
				List<String> characs = Arrays.asList(characters.split("\n"));
				int num_chars = 10;
				Set<String> finalCharacters = new HashSet<String>();
				while (finalCharacters.size() < num_chars) {
					int rand = (int) (Math.random() * characs.size());
					finalCharacters.add(characs.get(rand));
				}
				int numActs = (int) (Math.random() * 1) + 3;
				int act = 1;
				PrintWriter p = response.getWriter();
				httpget = new HttpGet(
						"http://ec2-50-17-57-153.compute-1.amazonaws.com:8080/Quizbowl_UI/titles.txt");
				r = httpclient.execute(httpget);
				entity = r.getEntity();
				sb = new StringBuilder();
				if (entity != null) {
					instream = entity.getContent();
					tmp = new byte[1000];
					while ((l = instream.read(tmp)) != -1) {
						sb.append(new String(tmp));
					}
				}
				String titles = sb.toString().replaceAll("\n", " ");
				String tTemp[] = titles.split(" ");
				String connectors[] = { "About", "Of", "For", "In" };
				int first = (int) (Math.random() * 2 + 1);
				int second = (int) (Math.random() * 2 + 1);
				String title = "";
				Set<String> firstTitle = new HashSet<String>();
				Set<String> secondTitle = new HashSet<String>();
				while (firstTitle.size() < first) {
					firstTitle.add(tTemp[(int) (Math.random() * tTemp.length)]
							+ " ");
				}
				while (secondTitle.size() < second) {
					secondTitle.add(tTemp[(int) (Math.random() * tTemp.length)]
							+ " ");
				}
				for (String string : firstTitle) {
					title += string;
				}
				title += connectors[(int) (Math.random() * connectors.length)]
						+ " ";
				for (String string : secondTitle) {
					title += string;
				}
				p.write(title += "\n\n\n");
				while (act <= numActs) {
					p.write("Act " + act + "\n\n\n");
					int numScenes = (int) (Math.random() * 4) + 1;
					int scenes = 1;
					while (scenes <= numScenes) {

						p.write("Scene " + scenes + "\n\n\n");
						int numConversations = (int) (Math.random() * 10 + 10);
						int conversations = 0;
						int numPeople = (int) (Math.random() * 3 + 2);
						int solScene = (int) (Math.random() * numConversations);
						Set<String> people = new HashSet<String>();
						while (people.size() < numPeople) {
							people.add(finalCharacters.toArray(new String[] {})[(int) (Math
									.random() * finalCharacters.size())]);
						}
						int personIndex = (int) (Math.random() * people.size());
						int lastPerson = -1;

						while (conversations < numConversations) {
							int numLines = 0;
							if (solScene == numConversations) {
								numLines = 10;
							} else {
								numLines = (int) (Math.random() * 4) + 1;
							}
							int lines = 0;
							String line = "";
							lastPerson = personIndex;
							while (personIndex == lastPerson) {
								personIndex = (int) (Math.random() * people
										.size());
							}
							while (lines < numLines) {
								int lineIndex = (int) (Math.random() * list
										.size());
								line += list.get(lineIndex);
								list.remove(lineIndex);
								lines++;
							}
							String person = people.toArray(new String[] {})[personIndex];
							p.write(person + ": " + line + "\n\n");
							conversations++;
						}
						scenes++;
					}
					act++;

				}

			}
		} catch (Throwable e) {
			try {
				response.getWriter().print(e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}
}
